<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="学校编号" prop="schoolNum">
        <el-input
          v-model="queryParams.schoolNum"
          clearable
          placeholder="请输入学校编号"
        ></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="schoolName">
        <el-select
          filterable
          v-model="queryParams.schoolName"
          placeholder="名称"
          clearable
        >
          <el-option
            v-for="item in schools"
            :key="item.schoolId"
            :label="item.schoolName"
            :value="item.schoolName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="级别" prop="levelId">
        <el-select
          filterable
          v-model="queryParams.levelId"
          placeholder="级别"
          clearable
        >
          <el-option
            v-for="item in level"
            :key="item.levelId"
            :label="item.levelName"
            :value="item.levelId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="auditStatus">
        <el-select
          v-model="queryParams.auditStatus"
          placeholder="状态"
          clearable
        >
          <el-option
            v-for="item in auditStatusList"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          v-preventReClick
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button
          v-preventReClick
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="编号" align="center" prop="schoolNum"/>
      <el-table-column label="logo" align="center" prop="schoolPic">
        <template slot-scope="scope">
          <img
            style="width: 80px; height: auto"
            :src="scope.row.schoolPic"
            alt="加载失败"
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="`${$schoolName ? $schoolName : '学校'}名称`"
        align="center"
        prop="schoolName"
      />
      <el-table-column label="级别" align="center" prop="levelName"/>
      <el-table-column label="状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <el-tag type="warning" v-if="scope.row.auditStatus == 0"
          >待审核
          </el-tag
          >
          <el-tag type="success" v-if="scope.row.auditStatus == 1"
          >已通过
          </el-tag
          >
          <el-tag type="danger" v-if="scope.row.auditStatus == 2"
          >已驳回
          </el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="提交时间" align="center" prop="startTime">
        <template slot-scope="scope">
          <div>{{ parseTime(scope.row.createTime).slice(0, 10) }}</div>
          <div>
            {{
              parseTime(scope.row.createTime).slice(
                10,
                parseTime(scope.row.createTime).length
              )
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="开始日期" align="center" prop="startTime">
      </el-table-column>
      <el-table-column label="结束日期" align="center" prop="endTime">
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.auditStatus == 0"
            v-preventReClick
            size="mini"
            type="text"
            @click="handleUpdate(scope.row, 0)"
          >审核
          </el-button
          >
          <el-button
            v-if="scope.row.auditStatus != 0"
            v-preventReClick
            size="mini"
            type="text"
            @click="handleUpdate(scope.row, 1)"
          >详情
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="diatype == 0 ? '审核' : '审核详情'"
      :visible.sync="open"
      width="520px"
      append-to-body
    >
      <el-form
        ref="submitForm"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="编号" prop="schoolNum">
          {{ form.schoolNum }}
        </el-form-item>
        <el-form-item label="名称" prop="schoolName">
          {{ form.schoolName }}
        </el-form-item>
        <el-form-item label="学校logo" prop="schoolPic">
          <div>
            <el-image
              width="120px"
              height="120px"
              :src="form.schoolPic"
              :preview-src-list="[form.schoolPic]"
            >
            </el-image>
          </div>
        </el-form-item>
        <el-form-item v-if="diatype == 1" label="级别" prop="levelName">
          {{ form.levelName }}
        </el-form-item>
        <el-form-item v-if="diatype == 1" label="起止时间">
          {{ form.startTime }} -- {{ form.endTime }}
        </el-form-item>
        <el-form-item v-if="diatype == 0" label="级别" prop="levelId">
          <el-select
            filterable
            class="level_add"
            v-model="form.levelId"
            placeholder="级别"
            clearable
          >
            <el-option
              v-for="item in level"
              :key="item.levelId"
              :label="item.levelName"
              :value="item.levelId"
            />
          </el-select>
        </el-form-item>
        <div v-if="diatype == 0" style="display: flex">
          <el-form-item label="起止日期" prop="startTime">
            <el-date-picker
              disabled
              class="startTime_add"
              value-format="yyyy-MM-dd HH:mm:ss"
              :picker-options="pickerOptionsEnd"
              v-model="form.startTime"
              type="date"
              clearable
              placeholder="开始日期"
            >
            </el-date-picker>
          </el-form-item>
          <span style="margin: 7px 10px 0">至</span>
          <el-form-item prop="endTime" label-width="0px">
            <el-date-picker
              class="startTime_add"
              :picker-options="pickerOptionsStart"
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="form.endTime"
              type="date"
              clearable
              placeholder="结束日期"
            >
            </el-date-picker>
          </el-form-item>
        </div>
        <el-form-item label="审核结果">
          <el-radio-group v-removeAriaHidden :disabled="diatype == 1" v-model="form.auditStatus">
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="2">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="凭证" prop="toPayPic" v-if="form.auditStatus == 1">
          <el-upload
            v-if="diatype == 0"
            class="avatar-uploader"
            :http-request="uploadFileToOSS"
            action="#"
            accept=".jpg,.jpeg,.png"
            :auto-upload="true"
            :show-file-list="false"
            :limit="1"
            list-type="picture"
            ref="upload"
          >
            <img v-if="form.toPayPic" :src="form.toPayPic" class="avatar"/>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <el-image
            v-if="diatype == 1"
            class="avatar"
            :src="form.toPayPic"
            :preview-src-list="[form.toPayPic]"
          >
          </el-image>
          <div
            v-if="diatype == 0"
            style="font-size: 12px; font-weight: 400; color: #a6a6a6"
          >
            支持JPG、PNG格式，图片小于5M
          </div>
        </el-form-item>
        <el-form-item
          label="驳回原因"
          prop="auditOpinion"
          v-if="form.auditStatus == 2"
        >
          <el-input
            :disabled="diatype == 1"
            maxlength="100"
            show-word-limit
            :rows="4"
            v-model="form.auditOpinion"
            type="textarea"
            placeholder="请输入驳回原因"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-right">
        <el-button v-preventReClick @click="cancel">{{
            diatype == 0 ? '取 消' : '关 闭'
          }}
        </el-button>
        <el-button
          v-if="diatype != 1"
          v-preventReClick
          type="primary"
          @click="submitForm"
        >提 交
        </el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getSchoolAuditList,
  putSchoolAudit
} from '@/api/financial/financialAudit'
import { listPost } from '@/api/school/level'
import { listPost_school } from '@/api/school/list'
import { uploadOSS } from '@/api/upload'
import { parseTime } from '@/utils/cc'

export default {
  name: 'Post',
  dicts: ['sys_normal_disable'],
  data() {
    return {
      auditStatusList: [
        {
          id: 0,
          value: '待审核'
        },
        {
          id: 1,
          value: '已通过'
        },
        {
          id: 2,
          value: '已驳回'
        }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      activeIndex: '',
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schoolName: undefined,
        schoolNum: '',
        levelId: undefined
      },
      schools: [],
      level: [],
      pickerOptionsStart: {
        // 时间只能大于startTime
        disabledDate: (time) => {
          if (!this.form.startTime) {
            return
          }
          return time.getTime() < new Date(this.form.startTime).getTime()
        }
      },
      pickerOptionsEnd: {
        // 时间只能小于startTime
        disabledDate: (time) => {
          if (!this.form.endTime) {
            return
          }
          return time.getTime() > new Date(this.form.endTime).getTime()
        }
      },
      diatype: 1,
      imageUrl: '',
      type: [
        { id: 0, name: '缺陷' },
        { id: 1, name: '通过' }
      ],
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      },
      // 表单参数
      form: {
        toPayPic: null,
        rejectReason: '',
        auditStatus: 1
      },
      // 表单校验
      rules: {
        levelId: [{ required: true, message: '级别不能为空', trigger: 'blur' }],
        startTime: [
          { required: true, message: '开始日期不能为空', trigger: 'blur' }
        ],
        endTime: [
          { required: true, message: '结束日期不能为空', trigger: 'blur' }
        ],
        toPayPic: [
          { required: true, message: '支付凭证不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    'form.auditStatus': {
      handler(val) {
        if (val == 1) {
          this.rules = {
            levelId: [
              { required: true, message: '级别不能为空', trigger: 'blur' }
            ],
            startTime: [
              { required: true, message: '开始日期不能为空', trigger: 'blur' }
            ],
            endTime: [
              { required: true, message: '结束日期不能为空', trigger: 'blur' }
            ],
            toPayPic: [
              { required: true, message: '支付凭证不能为空', trigger: 'blur' }
            ]
          }
          this.resetForm('submitForm')
        } else {
          this.rules = {
            levelId: [
              { required: true, message: '级别不能为空', trigger: 'blur' }
            ],
            startTime: [
              { required: true, message: '开始日期不能为空', trigger: 'blur' }
            ],
            endTime: [
              { required: true, message: '结束日期不能为空', trigger: 'blur' }
            ],
            auditOpinion: [
              { required: true, message: '拒绝原因不能为空', trigger: 'blur' }
            ]
          }
          this.resetForm('submitForm')
        }
      }
    }
  },
  created() {
    this.getList()
    this.getSchools()
    this.getLevel()
  },
  methods: {
    parseTime,
    uploadFileToOSS(file) {
      if (file.file.type.indexOf('image/') == -1) {
        this.$modal.msgError(
          '文件格式错误，请上传图片类型,如：JPG，PNG后缀的文件。'
        )
      } else {
        this.$refs.upload.clearFiles()
        uploadOSS(file.file).then((response) => {
          this.form.toPayPic = response[0]
        })
      }
    },
    getSchools() {
      listPost_school({
        pageNum: 1,
        pageSize: 9999,
        schoolId: this.$store.getters.schoolId
      }).then((response) => {
        this.schools = response.rows
      })
    },
    getLevel() {
      listPost({
        pageNum: 1,
        pageSize: 999
      }).then((response) => {
        this.level = response.rows
      })
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true
      getSchoolAuditList(this.queryParams).then((response) => {
        this.postList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.resetForm('submitForm')
      this.open = false
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.levelId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 操作按钮 */
    handleUpdate(row, type) {
      this.open = true
      this.diatype = type
      let data = JSON.parse(JSON.stringify(row))
      if (type == 0) {
        data.auditStatus = 1
        data.toPayPic = null
      }
      this.form = data
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['submitForm'].validate((valid) => {
        if (valid) {
          let data = JSON.parse(JSON.stringify(this.form))
          data.createTime = this.parseTime(data.createTime)
          putSchoolAudit(data).then(() => {
            this.$modal.msgSuccess('修改成功')
            this.open = false
            this.getList()
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.startTime_add {
  width: 160px;
}

.level_add {
  width: 354px;
}
</style>
